package me.shingohu.man.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import javax.inject.Inject;

import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.mvp.IPresenter;
import me.shingohu.man.util.ProgressHudUtil;

/**
 * Created by hujie on 17/4/15.
 */

public abstract class BaseMVPFragment<P extends IPresenter> extends BaseFragment {

    @Inject
    protected P mPresenter;
    protected BaseMVPActivity mActivity;
    protected ProgressHudUtil progressHudUtil;


    /**
     * 提供AppComponent(提供所有的单例对象)给子类，进行Component依赖
     * @param appComponent
     */
    protected abstract void setupActivityComponent(AppComponent appComponent);


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mActivity = (BaseMVPActivity) getActivity();
        progressHudUtil = new ProgressHudUtil(getActivity());
        setupActivityComponent(mActivity.mApplication.getAppComponent());
        onViewCreated(savedInstanceState);
    }


    protected abstract void onViewCreated(Bundle savedInstanceState);


    @Override
    protected void gc() {
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        if (progressHudUtil != null) {
            progressHudUtil.destory();
        }
        mPresenter = null;
        mActivity = null;

        super.gc();
    }
}
